﻿@model Huirui.Cavan.Presentation.ViewModel.StockModule.IStockAllocateViewModel
@{
    Layout = null;
}
@using (Html.BeginForm(ActionConst.ADD, ControllerConst.STOCKALLOCATE))
{
    <div class="tool_bar">
        <input id="stockallocate_save" type="submit" style="display: none;" />
        <input id="stockallocate_reset" type="reset" style="display: none" />
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'" onclick="javascript:$('#stockallocate_save').click()">保存</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">新增</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-delete'">删除</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-undo'" onclick="javascript:$('#stockallocate_reset').click()">重置</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'">查询</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-print'">打印</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-collapse'" onclick="toggleHeard()">折叠/展开</a>
    </div>
    <table class="form_heard">
        <tr>
            <td class="c100">
                <label>调拨日期:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.AllocateDate, new { id = "allocateDate", style = "height:18px", @class = "Wdate", onfocus = "WdatePicker({minDate:'%y-%M-#{%d+1}'})" })
            </td>
            <td class="c100">
                <label>调出仓库:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.StockOut.WareHouse.Id, new { id = "outWareHouseId" })
            </td>
            <td class="c100">
                <label>调拨单号:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.ProductionNumber, new { id = "productionNumber" })
            </td>
            <td class="c100">
                <label>原始单号:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.ProductionNumber, new { id = "productionNumber" })
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label>备注:</label>
            </td>
            <td colspan="6">
                @Html.TextBoxFor(m => m.Item.Remark, new { id = "remark" })
            </td>
            <td class="c160">
                <a>请选择常用备注</a>
            </td>
        </tr>
    </table>
    @Html.HiddenFor(m => m.StockInOutDetails, new { id = "stockInOutDetails" })
}
<table id="selectedStocksForStockAllocate" title="存货列表">
</table>
<input type="button" value="选择商品" id="selectStockForStockAllocate" />
<input type="button" value="确定商品" id="doSelectStockForStockAllocate" />
<table id="stocksForStockAllocate" title="原始存货列表">
</table>
<script type="text/javascript">
    $(function ()
    {
        var lastIndex;
        var salesDetails;
        $('#selectedStocksForStockAllocate').datagrid({
            columns: [[
                { field: 'Id', title: '存货编码', width: 100 },
                { field: 'Name', title: '存货名称', width: 80 },
                { field: 'Specification', title: '规格', width: 60 },
                { field: 'UnitTransfer', title: '计量单位', width: 100 },
                { field: 'Number', title: '数量', width: 100, editor: { type: 'text'} },
                { field: 'AssistantNumber', title: '辅助数量', width: 100 },
                { field: 'StandardPurchasePrice', title: '单价', width: 100, editor: { type: 'text'} },
                { field: 'Cost', title: '成本 ', width: 80 },
                { field: 'Remark', title: '备注', width: 80 }
            ]]
        });

        $("#selectStockForStockAllocate").bind("click", function ()
        {
            $.ajax({
                url: '/stock/list',
                type: 'Post',
                success: function (result)
                {
                    if (result.total > 0)
                    {
                        $('#stocksForStockAllocate').datagrid({
                            columns: [[
                                { field: 'IsSelected', title: '选择', width: 60, checkbox: true },
                                { field: 'Type', title: '品类名称', width: 60 },
                                { field: 'Id', title: '存货编码', width: 100 },
                                { field: 'BarCode', title: '条形码', width: 80 },
                                { field: 'Name', title: '存货名称', width: 80 },
                                { field: 'Specification', title: '规格', width: 60 },
                                { field: 'UnitTransfer', title: '计量单位', width: 100 },
                                { field: 'StandardPurchasePrice', title: '单价', width: 80 }
                            ]],
                            data: result,
                            checkOnSelect: true,
                            selectOnCheck: true
                        });
                    }
                },
                error: function ()
                {
                    alert('系统繁忙，请稍候再试');
                }
            })

            return false;
        });

        var currentIndex;
        var currentRow;
        var rowId;
        var standardPurchasePrice;
        var number;

        $("#doSelectStockForStockAllocate").bind("click", function ()
        {
            $('#selectedStocksForStockAllocate').datagrid('loadData', { total: 0, rows: [] });
            var rows = $('#stocksForStockAllocate').datagrid('getSelections');
            for (var i = 0; i < rows.length; i++)
            {
                insert(rows[i].Id, rows[i].Name, rows[i].Specification, rows[i].UnitTransfer, 0, 0, rows[i].StandardPurchasePrice, 0, 'test');
            }
        });

        function insert(id, name, specification, unitTransfer, number, assistantNumber, standardPurchasePrice, cost, remark)
        {
            var rows = $('#selectedStocksForStockAllocate').datagrid('getRows');
            index = -1;
            if (rows.length == 0)
            {
                index = 0;
            }
            else
            {
                index = rows.length;
            }

            $('#selectedStocksForStockAllocate').datagrid('insertRow', {
                index: index,
                row: {
                    Id: id,
                    Name: name,
                    Specification: specification,
                    UnitTransfer: unitTransfer,
                    Number: number,
                    AssistantNumber: assistantNumber,
                    StandardPurchasePrice: standardPurchasePrice,
                    Cost: cost,
                    Remark: remark
                }
            });
            $('#selectedStocksForStockAllocate').datagrid('selectRow', index);
            $('#selectedStocksForStockAllocate').datagrid('beginEdit', index);

            var ed = $('#selectedStocksForStockAllocate').datagrid('getEditors', index);
            for (var i = 0; i < ed.length; i++)
            {
                var e = ed[i];
                if (e.field == "StandardSalesPrice")
                {
                    $(e.target).attr("selectedStockRowIndex", index);
                    $(e.target).bind("click", function ()
                    {
                        $('#selectedStocksForStockAllocate').datagrid('selectRow', $(this).attr("selectedStockRowIndex"));
                    });
                    $(e.target).bind("blur", function ()
                    {
                        standardPurchasePrice = $(this).val();
                        currentIndex = $(this).attr("selectedStockRowIndex");
                        currentRow = $('#selectedStocksForStockAllocate').datagrid('getSelected');
                        rowId = $(this).parents().filter("tr").filter("[datagrid-row-index=" + currentIndex + "]").attr("id");
                        $("#" + rowId).find("td").each(function ()
                        {
                            if ($(this).attr("field") == "Number")
                            {
                                number = $(this).find("input").val();
                            }
                            else if ($(this).attr("field") == "Cost")
                            {
                                $(this).find("div").html(standardPurchasePrice * number);
                            }
                        });
                        $('#selectedStocksForStockAllocate').datagrid('unselectRow', currentIndex);
                        currentRow.StandardPurchasePrice = standardPurchasePrice;
                    })
                }
                else if (e.field == "Number")
                {
                    $(e.target).attr("selectedStockRowIndex", index);
                    $(e.target).bind("click", function ()
                    {
                        $('#selectedStocksForStockAllocate').datagrid('selectRow', $(this).attr("selectedStockRowIndex"));
                    });
                    $(e.target).bind("blur", function ()
                    {
                        number = $(this).val();
                        currentIndex = $(this).attr("selectedStockRowIndex");
                        currentRow = $('#selectedStocksForStockAllocate').datagrid('getSelected');
                        rowId = $(this).parents().filter("tr").filter("[datagrid-row-index=" + currentIndex + "]").attr("id");
                        $("#" + rowId).find("td").each(function ()
                        {
                            if ($(this).attr("field") == "StandardPurchasePrice")
                            {
                                standardPurchasePrice = $(this).find("input").val();
                            }
                            else if ($(this).attr("field") == "Cost")
                            {
                                $(this).find("div").html(standardPurchasePrice * number);
                            }
                        });
                        $('#selectedStocksForStockAllocate').datagrid('unselectRow', currentIndex);
                        currentRow.Number = number;
                    })
                }
            }
        }

        $("form[action^='/stockallocate/add']").submit(function ()
        {
            var rows = $('#selectedStocksForStockAllocate').datagrid('getRows');
            stockInOutDetails = "[";
            for (var i = 0; i < rows.length; i++)
            {
                if (i == (rows.length - 1))
                {
                    stockInOutDetails = stockInOutDetails + "{ \"Stock\" : { \"Id\" : \"" + rows[i].Id + "\"}, " + "\"UnitCost\" :" + rows[i].StandardPurchasePrice + ", \"Number\" :" + rows[i].Number + " }";
                }
                else
                {
                    stockInOutDetails = stockInOutDetails + "{ \"Stock\" : { \"Id\" : \"" + rows[i].Id + "\"}, " + "\"UnitCost\" :" + rows[i].StandardPurchasePrice + ", \"Number\" :" + rows[i].Number + " },";
                }
            }
            stockInOutDetails = stockInOutDetails + "]";
            $("#stockInOutDetails").val(stockInOutDetails);
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (data)
                {
                    if (data.IsSuccess)
                    {
                        alert(data.Messages[0]);
                    }
                },
                error: function ()
                {
                    alert('系统繁忙，请稍候再试');
                }
            })

            return false;
        });
    });
</script>
